<?php

namespace app\hotel\controller;

use think\Controller;

class Guest extends Controller
{
    protected $business;
    public function _initialize()
    {

        parent::_initialize();
        $this->BusinessModel = model('Business.Business');
        $this->GuestModel = model('Hotel.Guest');

        $busid = $this->request->param('busid', 0, 'trim');
        $Business = $this->BusinessModel->find($busid);
        if(!$Business)
        {
            $this->error('用户不存在');
        }
        $this->business = $Business;
    }

    // 添加
    public function add()
    {
        if($this->request->isPost())
        {
            $gender = $this->request->param('gender', 0, 'trim');
            $mobile = $this->request->param('mobile', '', 'trim');
            $nickname = $this->request->param('nickname', '', 'trim');

            $data = [
                'gender' => $gender,
                'mobile' => $mobile,
                'nickname' => $nickname,
                'busid' => $this->business['id']
            ];

            // 加校验器
            $result = $this->GuestModel->save($data);

            if($result === false)
            {
                $this->error($this->GuestModel->getError());
                exit;
            }

            $this->success('添加成功');
        }
    }

    // 列表
    public function index()
    {
        if($this->request->isPost())
        {
            $page = $this->request->param('page', 1, 'trim');

            $list = $this->GuestModel->where(['busid' => $this->business['id']])->order('id desc')->page("$page,10")->select();
            
            if($list)
            {
                $this->success('', '', $list);
                exit;
            }
            else
            {
                $this->error('暂无数据');
                exit;
            }
        }
    }

    // 详情
    public function info()
    {
        if($this->request->isPost())
        {
            $gid = $this->request->param('gid', 0, 'trim');

            $info = $this->GuestModel->find($gid);

            if(!$info)
            {
                $this->error('住客信息不存在');
                exit;
            }

            $this->success('', '', $info);
        }
    }

    // 编辑
    public function edit()
    {
        $gender = $this->request->param('gender', 0, 'trim');
        $mobile = $this->request->param('mobile', '', 'trim');
        $nickname = $this->request->param('nickname', '', 'trim');
        $gid = $this->request->param('gid', 0, 'trim');

        $info = $this->GuestModel->find($gid);

        if(!$info)
        {
            $this->error('住客信息不存在');
            exit;
        }

        
        $data = [
            'id' => $gid,
            'gender' => $gender,
            'mobile' => $mobile,
            'nickname' => $nickname,
            'busid' => $this->business['id']
        ];

        // 加校验器
        $result = $this->GuestModel->isUpdate(true)->save($data);

        if($result === false)
        {
            $this->error($this->GuestModel->getError());
            exit;
        }

        $this->success('修改成功');
    }

    // 删除
    public function del()
    {
        $gid = $this->request->param('gid', 0, 'trim');

        $info = $this->GuestModel->find($gid);

        if(!$info)
        {
            $this->error('住客信息不存在');
            exit;
        }

        $result = $this->GuestModel->destroy($gid);

        if($result === false)
        {
            $this->error($this->GuestModel->getError());
            exit;
        }

        $this->success('删除成功');
    }
    
    // 获取住客列表
    public function guestlist()
    {
        if($this->request->isPost())
        {
            $busid = $this->request->param('busid', 0, 'trim');

            $Business = $this->BusinessModel->find($busid);

            if (!$Business)
            {
                $this->error('用户不存在');
                exit;
            }

            $list = $this->GuestModel->where(['busid' => $busid])->order('id desc')->select();

            $this->success('', '', $list);

        }
    }
}